//
// Created by xili on 2024/8/28 18:28.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    bool validateStackSequences(vector<int> &pushed, vector<int> &popped) {
        stack<int> s;
        int n = pushed.size();
        int i = 0, j = 0;
        for (; i < n; i++) {
            s.push(pushed[i]);
            while (!s.empty()&&s.top() == popped[j]) {
                j++;
                s.pop();
            }
        }
        while (!s.empty()) {
            if (s.top() != popped[j++]) {
                return false;
            }
            s.pop();
        }
        return true;
    }
};